1638A - Reverse - CodeForces Solution


constructive algorithms greedy math *800

Please click on ads to support us..

Python Code:

for s in[*open(0)][2::2]:
 a=[0,*map(int,s.split()),0];i=1
 while a[i]==i:i+=1
 if a[i]:j=a.index(i)+1;a[i:j]=a[i:j][::-1]
 print(*a[1:-1])

C++ Code:

//make longer column than row if Possible
//dont print always store ans
//prime:1e9+7
//use unorder map if just hashing is required
//if you know the size of vector then use array<int,size>
//continuously division by a number is log(n) operation.
/*
GO FIRST BRUTE FORCE..start with some homogeneous,simple testcases
TRY to DIVIDE problem into simple cases according to given conditions then prove your solution by using some Example for each case
SEE BIGGER PICTURE:take more time to think logic than code
If regularly wrong submission occurs,then check code line by line
*/
//string conversion to int=stoi(string),O(n)
#include<bits/stdc++.h>
#include<iostream>
typedef long long ll;
#define all(a) a.begin(),a.end()
#define ss second
#define ff first
#define pb push_back
#define el endl
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
int main()
{
fio;
ll tt;
cin>>tt;
while (tt--)
{
ll n;
cin>>n;
vector<ll>a(n),b;
int id=0;
vector<int>m(n+1);
for (int i = 0; i < n; i++){cin>>a[i];m[a[i]]=i;}
b=a;
sort(all(b));
// if(a==b){for(auto i:a)cout<<i<<" ";cout<<'\n';continue;}
for (int j = 0; j < n; j++)
{
if(a[j]==j+1){cout<<j+1<<" ";continue;}
id=m[j+1]; 

for (int i = id; i>=j; i--)
{
    cout<<a[i]<<" ";
}
for (int i = id+1; i < n; i++)
{
    cout<<a[i]<<" ";
}
break;
}
cout<<'\n';

}
return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST